﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Demo10_1_3
{
    internal class Program
    {
        private delegate int AddDelegate(int a, int b);
        static void Main(string[] args)
        {
            var add = new DynamicMethod("Add", typeof(int), new[] { typeof(int), typeof(int) });
            var il = add.GetILGenerator();
            il.Emit(OpCodes.Call, typeof(Debugger).GetMethod("Break"));
            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldarg_1);
            il.Emit(OpCodes.Add);
            il.Emit(OpCodes.Ret);
            var adddelegate = (AddDelegate)add.CreateDelegate(typeof(AddDelegate));
          //  Console.WriteLine("Function Pointer: 0x{0:x16}", Marshal.GetFunctionPointerForDelegate(adddelegate).ToInt64());
            Console.WriteLine(adddelegate(10, 20));
           
        }
    }
}
